//
//  XSexAlertView.m
//  ReceiveVouchers
//
//  Created by Veer on 2017/11/15.
//  Copyright © 2017年 ReceiveVouchers. All rights reserved.
//

#import "XSexAlertView.h"

#import "UIImage+VR.h"

@interface XSexAlertView()

@property (nonatomic, strong) NSMutableArray<UIButton *> *sexBtnArray;

@end

@implementation XSexAlertView

- (instancetype)init
{
    
    if (self = [super initWithTitle:@"请选择您的性别" sureBtnTitle:nil cancleBtnTitle:@"取消" size:CGSizeMake(280, 200)]) {
        
        self.sexBtnArray = [NSMutableArray array];
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    
    NSArray *sexTitleArray = @[@"男", @"女"];
    
    CGFloat margin = 25;
    CGFloat btnW = 70;
    CGFloat btnH = 37;
    for (int i = 0; i < 2; i++) {
        
        UIButton *sexBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sexBtn.tag = i;
        sexBtn.layer.cornerRadius = 2;
        sexBtn.layer.borderWidth = 0.5;
        sexBtn.layer.borderColor = RGB(193, 193, 193).CGColor;
        [sexBtn setBackgroundImage:[UIImage imageWithColor:RGB(244, 244, 244)] forState:UIControlStateNormal];
        [sexBtn setBackgroundImage:[UIImage imageNamed:@"button_background"] forState:UIControlStateHighlighted];
        [sexBtn setBackgroundImage:[UIImage imageNamed:@"button_background"] forState:UIControlStateSelected | UIControlStateHighlighted];
        [sexBtn setBackgroundImage:[UIImage imageNamed:@"button_background"] forState:UIControlStateSelected];
        [sexBtn setTitle:sexTitleArray[i] forState:UIControlStateNormal];
        [sexBtn setTitleColor:RGB(34, 34, 34) forState:UIControlStateNormal];
        [sexBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [sexBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [sexBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected | UIControlStateHighlighted];
        sexBtn.titleLabel.font = VRFont(14);
        [sexBtn addTarget:self action:@selector(sexBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:sexBtn];
        [self.sexBtnArray addObject:sexBtn];
        [sexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(84);
            if (i == 0) {
                make.right.mas_equalTo(self.alertView.mas_centerX).offset(-margin * 0.5);
            }else{
                make.left.mas_equalTo(self.alertView.mas_centerX).offset(margin * 0.5);
            }
            make.width.mas_equalTo(btnW);
            make.height.mas_equalTo(btnH);
        }];
    }
}

#pragma mark - action
- (void)sexBtnAction:(UIButton *)sender
{
    BOOL isHadSelected = sender.selected;
    
    for (UIButton *btn in self.sexBtnArray) {
        
        if (btn == sender) {
            
            btn.layer.borderWidth = 0;
            btn.selected = YES;
        }else{
            
            btn.layer.borderWidth = 0.5;
            btn.selected = NO;
        }
    }
    
    if (self.SelectedBlock && !isHadSelected) {
        self.SelectedBlock(sender.tag);
    }
    
    [self dissmiss];
}

- (void)setDefualtSelectedIndex:(NSInteger)defualtSelectedIndex
{
    for (UIButton *btn in self.sexBtnArray) {
        
        if (btn.tag == defualtSelectedIndex) {
            
            btn.layer.borderWidth = 0;
            btn.selected = YES;
        }else{
            
            btn.layer.borderWidth = 0.5;
            btn.selected = NO;
        }
    }
}

@end
